﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Data;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class RegCompanyAndAreaEdit : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.QueryString["companyId"]))
                {
                    PageError("非法路径！", "");
                }
                else
                {
                    setBindConmpanyInfo();
                    setBindDeliveryArea();
                }
            }
        }

        IBLL_Area iArea = Factory.BLL_Area();
        IBLL_SelectArea iSelectArea = Factory.BLL_SelectArea();
        IBLL_SelectRelation iSelectRelation = Factory.BLL_SelectRelation();
        IBLL_Company companyCtrl = Factory.BLL_Company();

        /// <summary>
        /// 绑定所有符合条件的采购目录
        /// </summary>
        private void setBindConmpanyInfo()
        {
            string companyId = Request.QueryString["companyId"];
            HZ.DrugTrade.Model.CompanyInfo cominfo = companyCtrl.GetCompanyInfoByID(companyId);
            if (cominfo != null)
            {
                txtAddress.Text = cominfo.Address;
                txtBusinessCertificate.Text = cominfo.BusinessCertificate;
                txtBusinessCertificateDate.Text = cominfo.BusinessCertificateDate.HasValue ? cominfo.BusinessCertificateDate.Value.ToString("yyyy-MM-dd") : "--";
                txtBusinessRange.Text = cominfo.BusinessRange;
                txtcompanyname.Text = cominfo.CompanyName;
                txtContactPerson.Text = cominfo.ContactPerson;
                txtFax.Text = cominfo.Fax;
                txtGSPCode.Text = cominfo.GSPCode;
                txtGSPTime.Text = cominfo.GSPTime.HasValue ? cominfo.GSPTime.Value.ToString("yyyy-MM-dd") : "--";
                txtLayMan.Text = cominfo.LayMan;
                txtCellNo.Text = cominfo.CellNo;
                txtRemarks.Text = cominfo.Remarks;
                txtTelNo.Text = cominfo.TelNo;
                txtWebsite.Text = cominfo.Website;

                hidAddId.Value = cominfo.AddpersonID;

                if (cominfo.AddpersonID == null || cominfo.AddpersonID != CurrentAccount.GetUser().Org.OrgID)
                {
                    txtAddress.Enabled = false;
                    txtBusinessCertificate.Enabled = false;
                    txtBusinessCertificateDate.Enabled = false;
                    txtBusinessRange.Enabled = false;
                    txtcompanyname.Enabled = false;
                    txtContactPerson.Enabled = false;
                    txtFax.Enabled = false;
                    txtGSPCode.Enabled = false;
                    txtGSPTime.Enabled = false;
                    txtLayMan.Enabled = false;
                    txtCellNo.Enabled = false;
                    txtRemarks.Enabled = false;
                    txtTelNo.Enabled = false;
                    txtWebsite.Enabled = false;
                }
                //IBLL_User iu = Factory.BLL_User();
                //int TotalCount = 0;
                //IList<HZ.DrugTrade.Model.View.OrgUsers> listUser = iu.GetOrgUsersByWhere(new Model.Query.OrgUsers() { RoleID = "02", OrgID = companyId, SortFields = " LastLoginTime DESC" }, 1, 0, out TotalCount);
                ////置空用户IDs字符串
                //if (listUser.Count > 0)
                //{
                //    HZ.DrugTrade.Model.View.OrgUsers user = listUser[0];
                //    txtLastlogin.Text = user.LastLoginTime.HasValue ? user.LastLoginTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "<font color=red>从未登录过</font>";
                //}
                //else
                //{
                //    lblLastlogin.Text = "<font color=red>从未登录过</font>";
                //}
            }
            else
            {
                PageError("企业不存在！", "");
            }
        }
        /// <summary>
        /// 绑定所有符合条件的采购目录
        /// </summary>
        private void setBindDeliveryArea()
        {
            string companyId = Request.QueryString["companyId"];
            Model.Area aArea = CurrentAccount.GetCurrentDeployArea();  //获取当前实施地区的地区

            IList<Model.Area> aAreaList = iArea.GetAllCity(aArea.AreaID);
            gvwArea.DataSource = aAreaList;
            gvwArea.DataBind();

            IList<Model.SelectArea> sSelectAreaList = iSelectArea.GetAllCity(companyId);

            IList<Model.SelectRelation> sSelectRelationList = iSelectRelation.GetAllSelectRelationByCompanyPSID(companyId);

            string strPSAreas = "";
            foreach (Model.SelectRelation item in sSelectRelationList)
            {
                if (strPSAreas.Contains(item.AreaID) == false)
                {
                    strPSAreas += item.AreaID+"'";
                }
            }

            if (sSelectAreaList.Count > 0)
            {
                hidStatus.Value = "1";
                for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
                {
                    CheckBox ckb=(CheckBox)gvwArea.Rows[_index].FindControl("checkbox1");
                    if (sSelectAreaList[0].AreaIDs.Contains(this.gvwArea.DataKeys[_index].Value.ToString().ToUpper()))
                    {
                        ckb.Checked = true;

                        if (strPSAreas.Contains(this.gvwArea.DataKeys[_index].Value.ToString().ToUpper()))
                        {
                            ckb.Enabled = false;
                        }
                    }
                    
                }
            }
            else
            {
                hidStatus.Value = "0";
            }
        }


        protected void btnEdit_Click(object sender, EventArgs e)
        {
            string code = this.txtCode.Text;
            string companyId = Request.QueryString["companyId"];

            if (string.IsNullOrEmpty(hidAddId.Value)==false && hidAddId.Value == CurrentAccount.GetUser().Org.OrgID)
            {
                if (string.IsNullOrEmpty(this.txtGSPCode.Text.Trim()))
                {
                    HZ.Web.WebUtil.Alert("GSP认证编号不能为空！");
                    return;
                }
            }

            if (CurrentAccount.VerifyValidateCode(code))
            {
                if (new HZ.Common.ConvertHelper(txtcompanyname.Text.Trim()).String != string.Empty && new HZ.Common.ConvertHelper(txtCellNo.Text.Trim()).String != string.Empty && new HZ.Common.ConvertHelper(txtContactPerson.Text.Trim()).String != string.Empty)
                {
                    Company company = new Company();
                    company.CompanyID = companyId;
                    company.CompanyName = txtcompanyname.Text.Trim();
                    company.CompanyType = EnumCompanyType.配送企业;

                    CompanyInfo compinfo = new CompanyInfo();
                    compinfo.CompanyID = companyId;
                    compinfo.CompanyName = txtcompanyname.Text.Trim();
                    compinfo.TelNo = txtTelNo.Text.Trim();
                    compinfo.ContactPerson = txtContactPerson.Text.Trim();
                    compinfo.CellNo = txtCellNo.Text.Trim();
                    compinfo.LayMan = txtLayMan.Text.Trim();
                    compinfo.Address = txtAddress.Text.Trim();
                    compinfo.BusinessCertificate = txtBusinessCertificate.Text.Trim();
                    compinfo.BusinessCertificateDate = new HZ.Common.ConvertHelper(txtBusinessCertificateDate.Text.Trim()).ToDateTime;
                    compinfo.Website = txtWebsite.Text.Trim();
                    compinfo.GSPCode = txtGSPCode.Text.Trim();
                    compinfo.GSPTime = new HZ.Common.ConvertHelper(txtGSPTime.Text.Trim()).ToDateTime;
                    compinfo.BusinessRange = txtBusinessRange.Text.Trim();
                    compinfo.Remarks = txtRemarks.Text.Trim();

                    SelectArea sArea = new SelectArea();
                    //获取所有选择的目录
                    string ids = ",";

                    //遍历列表的所有行
                    for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
                    {

                        CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                        if (ckb.Checked)
                        {
                            ids += this.gvwArea.DataKeys[_index].Value.ToString() + ",";
                        }
                    }

                    sArea.AreaIDs = ids;
                    sArea.CompanyID_PS = companyId;

                    IBLL_Company companydb = Factory.BLL_Company();
                    int result = companydb.UpdateCompanyInfo(company, compinfo, sArea);
                    if (result==1)
                    {
                        WebHint.ShowInfo("提交成功！", "CompanyMaintain.aspx");
                    }
                    else if (result == 2)
                    {
                        HZ.Web.WebUtil.Alert("“企业名称”或“经营许可证号”已经存在！", this);
                    }
                    else
                    {
                        WebHint.ShowError("提交失败！", "");
                    }
                }
                else
                {
                    HZ.Web.WebUtil.Alert("请填写必填项！", this);
                }
            }
            else
            {
                this.txtCode.Text = "";
                HZ.Web.WebUtil.Alert("验证码不正确！", this);
            }
        }

    }
}
